﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Code.DAL;
using System.Data.SqlClient;
using System.Data;

namespace Jungle.Code.News
{
    public class News
    {
        public string err = ""; //основной поток ошибок
        private string ConnectionString = DBHelper.ConnectionString;

        public List<News> NewsList;

        #region Fields
        private int id_news;
        private string title;
        private string new_body;
        private DateTime pubDate;
        private string img;
        private int categoryId;
        #endregion

        #region Properties
        public int IdNews { get { return id_news; } set { id_news = value; } }
        public string Title { get { return title; } set { title = value; } }
        public string Body { get { return new_body; } set { new_body = value; } }
        public DateTime PubDate { get { return pubDate; } set { pubDate = value; } }
        public string Img { get { return img; } set { img = value; } }
        public int CategoryId { get { return categoryId; } set { categoryId = value; } }
        #endregion

        public void Add()
        {
            SqlConnection connect = new SqlConnection(ConnectionString);
            SqlCommand command = new SqlCommand("dbo.mdNews_Add", connect);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                command.Parameters.Add("@title", SqlDbType.VarChar).Value = Title;
                command.Parameters.Add("@body", SqlDbType.Text).Value = Body;
                command.Parameters.Add("@Img", SqlDbType.VarChar).Value = Img;
                command.Parameters.Add("@CategoryId", SqlDbType.Int).Value = CategoryId;

                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                err = ex.ToString();

            }
            finally
            {
                command.Connection.Close();
            }
        }
        //---------------------------------------------------------------------------------
        public void Delete()
        {
            SqlConnection connect = new SqlConnection(ConnectionString);
            SqlCommand command = new SqlCommand("dbo.mdNews_Delete", connect);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                command.Parameters.Add("@ID", SqlDbType.Int).Value = IdNews;
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                err = ex.ToString();

            }
            finally
            {
                command.Connection.Close();
            }
        }
        //---------------------------------------------------------------------------------
        public void Update()
        {

            SqlConnection connect = new SqlConnection(ConnectionString);
            SqlCommand command = new SqlCommand("dbo.mdNews_Update", connect);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                command.Parameters.Add("@id_news", SqlDbType.Int).Value = IdNews;
                command.Parameters.Add("@title", SqlDbType.VarChar).Value = Title;
                command.Parameters.Add("@body", SqlDbType.Text).Value = Body;
                command.Parameters.Add("@Img", SqlDbType.VarChar).Value = Img;
                command.Parameters.Add("@CategoryId", SqlDbType.Int).Value = CategoryId;

                
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                err = ex.ToString();

            }
            finally
            {
                command.Connection.Close();
            }
        
        }
        //---------------------------------------------------------------------------------
        public int GetAll()
        {
            int count = 0;
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                NewsList = new List<News>();                
                SqlCommand command = new SqlCommand("dbo.mdNews_GetAllNews", connect);
                command.CommandType = CommandType.StoredProcedure;
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;

                command.Connection.Open();
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    News n = new News();
                    n.IdNews = (int)reader["id_news"];
                    n.Body = reader["body"].ToString().Trim();
                    n.Title = reader["title"].ToString().Trim();
                    n.pubDate = (DateTime)reader["pubDate"];
                    n.Img = reader["Img"].ToString().Trim();
                    n.CategoryId = (int)reader["CategoryId"];

                    NewsList.Add(n);

                    count++;
                }

                reader.Close();
                command.Connection.Close();

            }
            return count;
        }
        //---------------------------------------------------------------------------------
        public void GetById()
        {
            
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                NewsList = new List<News>();
                SqlCommand command = new SqlCommand("dbo.mdNews_GetById", connect);
                command.CommandType = CommandType.StoredProcedure;
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID", SqlDbType.Int).Value = id_news;

                command.Connection.Open();
                reader = command.ExecuteReader();

                while (reader.Read())
                {                    
                    IdNews = (int)reader["id_news"];
                    Body = reader["body"].ToString().Trim();
                    Title = reader["title"].ToString().Trim();
                    pubDate = (DateTime)reader["pubDate"];
                    Img = reader["Img"].ToString().Trim();
                    CategoryId = (int)reader["CategoryId"];
                    
                }

                reader.Close();
                command.Connection.Close();

            }
            
        }
        //---------------------------------------------------------------------------------
        public int Search(string SearchStr)
        {
            int count = 0;
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                NewsList = new List<News>();
                SqlCommand command = new SqlCommand("dbo.mdNews_Search", connect);
                command.CommandType = CommandType.StoredProcedure;
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@Argument", SqlDbType.VarChar).Value = SearchStr;

                command.Connection.Open();
                reader = command.ExecuteReader();

                if (reader == null) { return 0; }

                while (reader.Read())
                {
                    News n = new News();
                    n.IdNews = (int)reader["id_news"];                 

                    NewsList.Add(n);

                    count++;
                }

                reader.Close();
                command.Connection.Close();

            }
            return count;
        }
    }
}
